package com.design.blog.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.design.blog.dao.ArticleMapper;
import com.design.blog.dao.CommentMapper;
import com.design.blog.dao.UserinfoMapper;
import com.design.blog.model.Comment;
import com.design.blog.model.vo.CommentParams;
import com.design.blog.model.R;
import com.design.blog.model.Userinfo;
import com.design.blog.model.vo.CommentVO;
import com.design.blog.model.vo.PageNationParams;
import com.design.blog.model.vo.PageResult;
import com.design.blog.service.ICommentService;
import com.design.blog.service.ThreadService;
import com.design.blog.utils.JWTUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @author hxg
 * @date 2022-02-28
 *  服务实现类
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {
    @Resource
    private CommentMapper commentMapper;

    @Resource
    private UserinfoMapper userInfoMapper;

    @Resource
    private ThreadService threadService;

    @Resource
    private ArticleMapper articleMapper;

	@Override
	public IPage<Comment> selectCommentPage(IPage<Comment> page, CommentVO comment) {
		return page.setRecords(baseMapper.selectCommentPage(page, comment));
	}

    @Override
    public List<CommentVO> getArticleCommentList(Long id) {
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getArticleId, id);
        wrapper.orderByDesc(Comment::getCreateTime);
        List<Comment> comments = commentMapper.selectList(wrapper);
        List<CommentVO> commentsVo = new ArrayList<>();
        for (Comment comment : comments) {
            CommentVO commentVO = new CommentVO();
            Userinfo userinfo = userInfoMapper.selectOne(new LambdaQueryWrapper<Userinfo>().eq(Userinfo::getId, comment.getAuthorId()));
            BeanUtils.copyProperties(comment, commentVO);
            if (userinfo.getAccount() != null) {
                commentVO.setAccount(userinfo.getAccount());
            }
            commentsVo.add(commentVO);
        }
        return commentsVo;
    }

    @Override
    public R comment(String token,CommentParams params) {
        Map<String, String> tokenMap = JWTUtils.checkToken(token);
        String userId = tokenMap.get("userId");
        Comment comment = new Comment();
        comment.setArticleId(params.getArticleId());
        comment.setAuthorId(userId);
        comment.setContent(params.getContent());
        String parent = params.getParent();
        if (parent == null ) {
            comment.setLevel("1");
        }else{
            comment.setLevel("2");
        }
        comment.setParentId(parent);
        String toUserId = params.getToUserId();
        comment.setToUid(toUserId == null ? "0" : toUserId);
        this.commentMapper.insert(comment);
        //异步更新文章的评论数
        threadService.updArticleComments(articleMapper,params.getArticleId());
        return R.success(null);
    }

    @Override
    public PageResult<CommentVO> getAllUseComment(PageNationParams params) {
        Page<Comment> page = new Page<>(params.getCurrentPage(),params.getPageSize());
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(params.getQueryString())){
            queryWrapper.like(Comment::getContent,params.getQueryString());
        }
        Page<Comment> userPage = commentMapper.selectPage(page, queryWrapper);
        List<Comment> records = userPage.getRecords();
        List<CommentVO> recodsVO=new ArrayList<>();
        for (Comment comment : records) {
            String authorId = comment.getAuthorId();
            Userinfo userinfo = userInfoMapper.selectById(authorId);
            CommentVO commentVO = new CommentVO();
            BeanUtils.copyProperties(comment, commentVO);
            commentVO.setAccount(userinfo.getAccount());
            recodsVO.add(commentVO);
        }

        PageResult<CommentVO> pageResult = new PageResult<>();
        pageResult.setList(recodsVO);
        pageResult.setTotal(userPage.getTotal());
        return pageResult;
    }

}
